package com.itheima.day20.demo05;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Test {
    public static final String sourcePath = "D:\\day04\\A星算法";// 填写【源文件夹】的绝对路径
    public static final String path = "D:\\day04\\123";// 填写【目标文件夹】的绝对路径(若不存在，自动创建)

    public static void main(String[] args) throws IOException {
        copyDir(sourcePath, path);
    }

    public static void copyDir(String oldPath, String newPath) throws IOException {
        File file = new File(oldPath);        //文件名称列表
        String[] filePath = file.list();

        if (!(new File(newPath)).exists()) {
            (new File(newPath)).mkdir();
            System.out.println("创建文件夹:" + newPath);
        }

        for (int i = 0; i < filePath.length; i++) {
            System.out.println(filePath[i]);
            if ((new File(oldPath + file.separator + filePath[i])).isDirectory()) {
                copyDir(oldPath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
            }

            if (new File(oldPath + file.separator + filePath[i]).isFile()) {
                copyFile(oldPath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
            }

        }
    }

    public static void copyFile(String oldPath, String newPath) throws IOException {
        File oldFile = new File(oldPath);
        File file = new File(newPath);
        FileInputStream in = new FileInputStream(oldFile);
        FileOutputStream out = new FileOutputStream(file);
        ;

        byte[] buffer = new byte[2097152];

        while ((in.read(buffer)) != -1) {
            out.write(buffer);
        }
    }
}
